package com.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ddd.wandemo.R;
import com.util.DensityUtils;

import de.hdodenhof.circleimageview.CircleImageView;

public class SelectThemeItem extends RelativeLayout {
    private int themeColor;
    private String themeName;
    private boolean isChecked = false;
    private CircleImageView cirTheme;
    private TextView tvTheme, tvUsing;
    private OnClickListener mOnClickListener;

    public SelectThemeItem(Context context) {
        this(context, null);
    }

    public SelectThemeItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectThemeItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectThemeItem);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.SelectThemeItem_theme_name) {
                themeName = typedArray.getString(attr);
            } else if (attr == R.styleable.SelectThemeItem_select_theme_color) {
                themeColor = typedArray.getColor(attr, context.getResources().getColor(R.color.colorAccent));
            } else if (attr == R.styleable.SelectThemeItem_is_checked) {
                isChecked = typedArray.getBoolean(attr, false);
            }
        }
        typedArray.recycle();

        View selectView = LayoutInflater.from(context).inflate(R.layout.item_select_theme, null, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 55));
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(selectView, params);

        cirTheme = selectView.findViewById(R.id.cir_color);
        tvTheme = selectView.findViewById(R.id.tv_color);
        tvUsing = selectView.findViewById(R.id.tv_using);

        cirTheme.setImageDrawable(new ColorDrawable(themeColor));
        tvTheme.setTextColor(themeColor);
        tvTheme.setText(themeName);

        setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onThemeClick();
                }
            }
        });

    }

    public void setThemeUse(Context context) {
        isChecked = !isChecked;
        if (isChecked) {
            tvUsing.setText("使用中");
            tvUsing.setSelected(true);
        } else {
            tvUsing.setText("使用");
            tvUsing.setSelected(false);
        }
    }


    public interface OnClickListener {
        void onThemeClick();
    }

    public void setOnClickThemeListener(OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }

    //bg : context.getResources().getDrawable(R.drawable.bg_item_theme)
    private StateListDrawable getStateDrawable(Context context, Drawable selectBg, Drawable normalBg) {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_selected}, selectBg);
        states.addState(new int[]{android.R.attr.state_pressed}, selectBg);
        states.addState(new int[]{android.R.attr.state_checked}, selectBg);
        states.addState(new int[]{}, normalBg);
        return states;
    }

    private ColorStateList getStateColor(Context context, int state1, int state2, int color1, int color2) {
        int[][] states = new int[][]{
                new int[]{state1}, // unchecked  -android.R.attr.state_checked
                new int[]{state2}  // checked android.R.attr.state_checked
        };//把两种状态一次性添加

        int[] colors = new int[]{
                color1,
                color2
        };//把两种颜色一次性添加
        ColorStateList colorStates = new ColorStateList(states, colors);
        return colorStates;
    }

}
